/* resit数据库操作 */
const mongoose = require('mongoose');

const url = {
    scheme: 'mongodb',
    host: 'localhost',
    port: 27017,
    database: 'resit',
    join() {
        return `${this.scheme}://${this.host}:${this.port}/${this.database}`;
    }
}

// 用于定义重考的信息
const resitSchema = new mongoose.Schema({
    stuID: String,
    stuName: String,
    college: String,
    class: String,
    course: String,
    credit: String,
    curriculum: String,
    jobNum: String,
    tName: String,
    strtCollege: String,
    grade: String,
    testDate: String,
    testTime: String,
    lieuTest: String,
    notes: String
});

// 用于生成重考信息的类
const ResitModel = mongoose.model('Resit', resitSchema);


function connect() {
    mongoose.connect(`${url.join()}`);
    let resit = mongoose.connection;
    resit.on('error', console.error.bind(console, 'error'));
    resit.once('open', function () {
        console.log(`${url.join()} 数据库连接成功！`);
    });
}


// 查询该学号下的所有补考信息
function findResits(stuID) {
    return new Promise((resolve, reject) => {
        ResitModel.find({ stuID }, '-_id', function (err, res) {
            if (err) {
                reject(err);
            } else {
                resolve(res);
            }
        });
    })
        .catch(err => {
            console.log(err);
        });
}




module.exports.url = url;
module.exports.connect = connect;
module.exports.findResits = findResits;